function Validate(arg) {

    var doc = document,
        validaingFalseElemet = {},
        postfix = 'x',

    errorText = {
        require: function (name) {
            return message[name + '_require'] ? message[name + '_require'] : message.require;
        },
        max: function (name, max) {
            return String.format(message[name + '_max'] ? message[name + '_max'] : message.max, max);
        }
    },

          message = {
              require: '不能为空',
              max: '最多{0}个字',
              reg: '格式不正确'
          },
          check = {
              require: function (elem) {

                  var isOK = true;

                  var defaultData = elem.getAttribute('data-default');
                  var name = elem.name;
                  if (validaingFalseElemet[name] && validaingFalseElemet[name] != 'require') {
                      return;
                  }
                  if (/\S+/.test(elem.value) && defaultData != elem.value) {
                      clearError(name);
                      validaingFalseElemet[name] = false;
                  }
                  else {
                      setError(elem.name, errorText.require(elem.name));
                      validaingFalseElemet[name] = 'require';
                      isOK = false;
                  }
                  return isOK;
              },
              max: function (elem) {
                  var max,
                      isOk = true;

                  var name = elem.name;
                  if (validaingFalseElemet[name] && validaingFalseElemet[name] != 'max') {
                      return;
                  }
                  max = elem.getAttribute('data-max');

                  if (elem.value.length > max) {
                      setError(elem.name, errorText.max(name, max));
                      validaingFalseElemet[name] = 'max';
                  }
                  else {

                      clearError(elem.name);
                      validaingFalseElemet[name] = false;
                  }
              },
              submitRequire: function (elems) {
                  var isOk = true, isGroupOk, firstElemName;

                  for (var i = 0; i < elems.length; i++) {
                      isGroupOk = false;
                      firstElemName = arg.form[elems[i][0]].name;


                      for (var j = 0; j < elems[i].length; j++) {

                          if (/\S+/.test(arg.form[elems[i][j]].value)) {
                              isGroupOk = true;

                              break;
                          }
                      }

                      if (isGroupOk) {
                          clearError(firstElemName);

                      }
                      else {
                          isOk = false;

                          setError(firstElemName, errorText.require(firstElemName));

                      }
                  }
                  return isOk;
              },
              submitRequireForInit: function (elem) {

                  var isGroupOk, firstElemName, elems = arg.data.submitRequire;

                  var name = elem.name;
                  if (validaingFalseElemet[name] && validaingFalseElemet[name] != 'require') {
                      return;
                  }
                  for (var i = 0; i < elems.length; i++) {
                      if (!checkInArray(name, elems[i])) {
                          continue;
                      }
                      isGroupOk = false;
                      firstElemName = arg.form[elems[i][0]].name;

                      for (var j = 0; j < elems[i].length; j++) {

                          if (/\S+/.test(arg.form[elems[i][j]].value)) {
                              isGroupOk = true;
                              break;
                          }
                      }


                  }
                  if (isGroupOk) {
                      clearError(firstElemName);
                      validaingFalseElemet[name] = false;
                  }
                  else {

                      setError(firstElemName, errorText.require(firstElemName));
                      validaingFalseElemet[name] = 'require';
                  }

              }
          };
    function setPostfixV(pf) {
        postfix = pf;
    }
    init();
    function checkInArray(item, array) {
        var isIn = false;
        for (var i = 0; i < array.length; i++) {
            if (array[i] == item) {
                isIn = true;
                break;
            }
        }
        return isIn;
    }
    function init() {

        if (arg.message) {
            Object.extend(message, arg.message);
        }

        var data = arg.data.require, i = 0;
        for (i = 0; i < data.length; i++) {
            bindEvent(arg.form[data[i]], 'blur', function (e) { check.require(getTarget(e)); });
            bindEvent(arg.form[data[i]], 'keydown', function (e) {
                e = getTarget(e);
                window.setTimeout(function () {
                    check.require(e);
                },
                100);
            });
        }
        data = arg.data.max;
        for (i = 0; i < data.length; i++) {
            bindEvent(arg.form[data[i]], 'blur', function (e) { check.max(getTarget(e)); });
        }
        data = arg.data.submitRequire;
        for (i = 0; i < data.length; i++) {
            for (var j = 0; j < data[i].length; j++) {
                bindEvent(arg.form[data[i][j]], 'keydown', function (e) {
                    e = getTarget(e);
                    window.setTimeout(function () {
                        check.submitRequireForInit(e);
                    },
                100);
                });
                bindEvent(arg.form[data[i][j]], 'blur', function (e) {
                    check.submitRequireForInit(getTarget(e));
                });
            }
        }
    }

    return {
        validate: function () {

            var data = arg.data.submitRequire, i = 0, isOk = true;

            if (!check.submitRequire(data)) {
                isOk = false;
            }


            data = arg.data.require;

            for (i = 0; i < data.length; i++) {
                if (!check.require(arg.form[data[i]])) {
                    isOk = false;
                }
            }
            data = arg.data.max;
            for (i = 0; i < data.length; i++) {
                if (check.max(arg.form[data[i]])) {
                    isOk = false;
                }
            }

            return isOk;
        },
        setPostfix: function (pf) {

            setPostfixV(pf);

        }


    }
    function setError(name, text) {

        name = arg.nameMap[name] ? arg.nameMap[name] : name;
        doc.getElementById(name + '-' + postfix).innerHTML = text;

    }
    function clearError(name) {
        name = arg.nameMap[name] ? arg.nameMap[name] : name;
        doc.getElementById(name + '-' + postfix).innerHTML = '';
    }

    function getTarget(e) {
        e = e || window.event;
        return e.target || e.srcElement;
    }
}

